package com.base.vistter.iframe.inverter.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.entity.SysFirm;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.service.SysFirmService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/sys/firm")
public class SysFirmController {

    @Resource(name = "sysFirmService")
    private SysFirmService sysFirmService;

    @GetMapping
    public Result<Page<SysFirm>> get(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum, String name) throws IframeException {
        Page<SysFirm> page = Page.of(pageNo, pageNum);
        LambdaQueryWrapper<SysFirm> lambdaQueryWrapper = new LambdaQueryWrapper<SysFirm>();
        if (StringUtils.isNotEmpty(name)) {
            lambdaQueryWrapper.like(SysFirm::getName, name);
        }
        return Result.ok(sysFirmService.page(page, lambdaQueryWrapper));
    }

    @GetMapping("/{id}")
    public Result<SysFirm> get(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(sysFirmService.getById(id));
    }

    @PostMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> save(@RequestBody SysFirm sysFirm) throws IframeException {
        return Result.ok(sysFirmService.save(sysFirm));
    }

    @PutMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> update(@RequestBody SysFirm sysFirm) throws IframeException {
        return Result.ok(sysFirmService.update(sysFirm));
    }

    @DeleteMapping("/{id}")
    public Result<Integer> delete(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(sysFirmService.delete(id));
    }

    @GetMapping("/status")
    public Result<Boolean> status(@RequestParam("id") Integer id, @RequestParam("status") Integer status) throws IframeException {
        SysFirm sysFirm = new SysFirm();
        sysFirm.setId(id);
        sysFirm.setStatus(status);
        return Result.ok(sysFirmService.update(sysFirm));
    }
}
